package com.study.eg1;

import java.io.UnsupportedEncodingException;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

public class SyncProducer {

  public static void main(String[] args)
      throws UnsupportedEncodingException, InterruptedException, RemotingException, MQClientException, MQBrokerException {
    DefaultMQProducer producer = new DefaultMQProducer("GROUP_TEST");
    producer.setNamesrvAddr("localhost:9876");
    producer.start();
    producer.setRetryTimesWhenSendAsyncFailed(0);

    for (int i = 0; i < 10; i++) {
      Message message = new Message("TopicTest", "TagA",
          ("Hello MQ: " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
      producer.send(message, new SendCallback() {
        public void onSuccess(SendResult result) {
          System.out.printf("发送结果：%s%n", result);
        }

        public void onException(Throwable throwable) {
          System.err.println("发生异常：" + throwable.getMessage());
        }
      });

    }
  }

}
